package com.icez.baseutil

import android.app.PictureInPictureParams
import android.content.Intent
import android.graphics.Color
import android.graphics.PixelFormat
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION
import android.view.*
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.icez.base_util_library.*
import java.io.File


class MainActivity : AppCompatActivity() {
    var btn1: Button? = null
    var btn2: Button? = null
    var btn3: Button? = null
    var iv: ImageView? = null
    var pathStr: File? = null
    var et1: EditText? = null
    var runnable: Runnable? = null
    var layout: LinearLayout? = null
    var textView: TextView? = null
    var view: View? = null
    var btn4:LinearLayout ?= null
    var timeHandler: Handler = Handler(Looper.getMainLooper())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        RouteUtil.instance

        iv = findViewById(R.id.iv)
        layout = findViewById(R.id.layout)
        btn4 = findViewById(R.id.btn4)

        for (i in 0..20) {
            LogUtil.E("遍历" + i)
            val l = DateTimeUtil.instance.dateStrToTimestamp("2022-01-01 01:01", "yyyy-MM-dd HH:mm")
            LogUtil.E("时间：" + l.toString())
        }

        LogUtil.E("小时："+DateTimeUtil.instance.currentHour())

        textView = TextView(this)
        textView?.text = "我是悬浮窗口"
        textView?.setTextColor(Color.parseColor("#FFFFFF"))

        layout?.addView(textView, 0)

        var gson = Gson()
        var str = gson.toJson(test()).toString()
        var url = "ws://12222:26789/websocket?uniqueId=1"



        btn1 = findViewById(R.id.btn1)
        et1 = findViewById(R.id.et1)

        SoftKeyboardUtil.instance.addSoftKeyboardListener(
            this,
            0
        ) { isOpenSoft: Boolean, realSoftHeight: Int ->
            LogUtil.E("realSoftHeight: "+realSoftHeight)
            if(isOpenSoft){
//                btn4?.visibility = View.VISIBLE
                val lp = btn4?.layoutParams as RelativeLayout.LayoutParams

                lp.bottomMargin = realSoftHeight
                btn4?.layoutParams = lp
//                btn4?.setPadding(0,0,0,realSoftHeight)
            }else{
//                btn4?.visibility = View.GONE
                val lp = btn4?.layoutParams as RelativeLayout.LayoutParams

                lp.bottomMargin = 0
                btn4?.layoutParams = lp
            }


        }


        btn1?.setOnClickListener {
//            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
//            WebSocketUtil.instance.startConnect()
            SoftKeyboardUtil.instance.openSoftKeyboard(btn1)
//            if(runnable!=null){
//                timeHandler.removeCallbacks(runnable!!)
//            }
//            runnable = DateTimeUtil.instance.countUpTime(timeHandler){
//                    hour:Long,minute:Long,second:Long ->
//                btn1?.text = hour.toString()+" : "+minute+" : "+second
//            }

//            iv?.setImageBitmap(ViewUtil.instance.viewToBitmap(btn1))


//            WindowManagerUtil.instance.addFloatWindow(R.layout.window_layout)


        }

        btn2 = findViewById(R.id.btn2)
        btn2?.setOnClickListener {
//            if(view?.windowToken!=null){
//                windowManager.removeViewImmediate(view)
//            }
            WindowManagerUtil.instance.removeFloatWindow()
            Toast.makeText(this, "您点击了", Toast.LENGTH_SHORT).show()
//            SoftKeyboardUtil.instance.closeSoftKeyboard(this)
//            WebSocketUtil.instance.sendMessage("{ \"uniqueId\":1, \"cmd\":1, \"inface\":\"WebSocketServiceModel\",\"reqData:\":\"11111\",\"data\":\"123\" }")
        }

        btn3 = findViewById(R.id.btn3)
        btn3?.setOnClickListener {
//            WebSocketUtil.instance.closeConnect()
            val intent = Intent().apply {
                action = ACTION_MANAGE_OVERLAY_PERMISSION
            }
            startActivity(intent)
        }
    }

    data class test(
        val uniqueId: String = "1"
    )

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
//        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        val imageBitmap = data?.extras?.get("data")
//            iv?.setImageBitmap(imageBitmap)
        LogUtil.E("imageBitmap " + data)

//        }
    }


}